const Goods = require("../models/goods");
const goodsConversionService = require("../services/goodsConversionService");

//获取商品数据
exports.list = async function( page,size){
    page = Number(page);
    size = Number(size);
    return Goods.findAndCountAll({
        attributes:['id','title','img','score','num'],
        limit: size,
        offset: (page - 1) * size,
        order: [['num', 'DESC']],
    });
}


//获取最爆款的的两个商品
exports.heatGoods = async function(){
    return Goods.findAll({
        attributes:['id','title','img'],
        order: [['num', 'DESC']],
        limit:2
    })
}

//获取商品详情
exports.getDetail = async function(userId,id){
    const goods = await Goods.findByPk(id,{
        attributes:{exclude:['updatedAt','deletedAt']}
    });
    if(userId){
        let conversion = await goodsConversionService.getConversion(userId,id);
        if(conversion.length < 1){
            goods.dataValues.isConversion = 0;
        }else{
            goods.dataValues.isConversion = conversion[0].useState
        }
    }else{
        goods.dataValues.isConversion = 0;
    }
    
    return goods;
}
